package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import easier.recycler.BaseViewHolder;
import easier.recycler.MultiTypeAdapter;
import easier.recycler.TypeViewHolder;
import java.util.ArrayList;
import java.util.List;

public class MainActivity89 extends AppCompatActivity {

    private RecyclerView recycler;
    private RecyclerView recycler1;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity89.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main89 );
        initView();
    }

    private void initView() {

        recycler = (RecyclerView) findViewById( R.id.recycler );
        recycler1 = (RecyclerView) findViewById( R.id.recycler1 );

        recycler.setLayoutManager( new LinearLayoutManager( this ) );
        recycler1.setLayoutManager( new LinearLayoutManager( this, RecyclerView.HORIZONTAL, false ) );

        recycler.setAdapter( new MultiTypeAdapter( convert() ) );
        recycler1.setAdapter( new MultiTypeAdapter( convert() ) );

        PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView( recycler1 );

        VerticalSnapHelper snapHelper1 = new VerticalSnapHelper();
        snapHelper1.attachToRecyclerView( recycler );
    }

    private List< TypeViewHolder > convert() {

        int[] colors = new int[]{
            R.color.black,
            R.color.purple_200,
            R.color.purple_500,
            R.color.purple_700,
            R.color.teal_200,
            R.color.teal_700
        };

        ArrayList< TypeViewHolder > list = new ArrayList<>();
        for ( int i = 0; i < colors.length; i++ ) {
            int j = i;
            list.add( new BaseViewHolder( R.layout.activity_main89_item ) {
                @Override
                protected void onBind( ViewHolder viewHolder, View root, int position, int type, List< Object > payloads ) {

                    super.onBind( viewHolder, root, position, type, payloads );
                    root.setBackgroundColor( root.getResources().getColor( colors[j] ) );
                }
            } );
        }
        return list;
    }

    // 每次滑动滚动一个child距离,并且顶部对齐到recyclerView顶部
    private class VerticalSnapHelper extends PagerSnapHelper {

        private int[] mResult = new int[2];

        /**
         * 用于计算snap最终距离
         */
        @Nullable
        @Override
        public int[] calculateDistanceToFinalSnap(
            @NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView ) {

            mResult[1] = targetView.getTop();
            return mResult;
        }

        /**
         * 滑动后或者fling后回调,决定snap到哪个view
         */
        @Nullable
        @Override
        public View findSnapView( LayoutManager layoutManager ) {

            View targetNe = findTopMostViewNegative( layoutManager );
            View targetPo = findTopMostViewPositive( layoutManager );
            /* 找出最接近top的view */
            if ( targetNe != null && targetPo != null ) {
                int topN = targetNe.getTop();
                int topP = targetPo.getTop();
                if ( Math.abs( topN ) < Math.abs( topP ) ) {
                    return targetNe;
                } else {
                    return targetPo;
                }
            }
            return null;
        }

        /**
         * fling后回调,决定哪个snap到哪个位置
         */
        @Override
        public int findTargetSnapPosition(
            LayoutManager layoutManager, int velocityX, int velocityY ) {
            /* 向上滚动 */
            View target = null;
            int position = RecyclerView.NO_POSITION;
            if ( velocityY > 0 ) {
                target = findTopMostViewPositive( layoutManager );
                if ( target != null ) {
                    position = layoutManager.getPosition( target );
                }
            } else {
                target = findTopMostViewNegative( layoutManager );
                if ( target != null ) {
                    position = layoutManager.getPosition( target );
                }
            }
            return position;
        }

        /**
         * 找出recycler所有child中,top>0,并且最接近0的child,用于向上fling时
         *
         * @param layoutManager manager
         *
         * @return child
         */
        private View findTopMostViewPositive( LayoutManager layoutManager ) {

            int topMin = Integer.MAX_VALUE;
            View target = null;
            int childCount = layoutManager.getChildCount();
            for ( int i = 0; i < childCount; i++ ) {
                View child = layoutManager.getChildAt( i );
                int top = layoutManager.getDecoratedTop( child );
                if ( top > 0 && top < topMin ) {
                    topMin = top;
                    target = child;
                }
            }
            return target;
        }

        /**
         * 找出recycler所有child中,top<0,并且最接近0的child,用于向下fling时
         *
         * @param layoutManager manager
         *
         * @return child
         */
        private View findTopMostViewNegative( LayoutManager layoutManager ) {

            int topMin = Integer.MIN_VALUE;
            View target = null;
            int childCount = layoutManager.getChildCount();
            for ( int i = 0; i < childCount; i++ ) {
                View child = layoutManager.getChildAt( i );
                int top = layoutManager.getDecoratedTop( child );
                if ( top < 0 && top > topMin ) {
                    topMin = top;
                    target = child;
                }
            }
            return target;
        }
    }
}